<?php
/**
 * Created by JetBrains PhpStorm.
 * User: scala
 * Date: 12-10-24
 * Time: 上午8:59
 * To change this template use File | Settings | File Templates.
 */
class CommonAction extends Action
{
    /**
     *
     */
    function _initialize(){
        import('ORG.Util.Cookie');
        // 用户权限检查
        if (C('USER_AUTH_ON') && !in_array(MODULE_NAME, explode(',', C('NOT_AUTH_MODULE')))) {
            import('ORG.Util.RBAC');
            if (!RBAC::AccessDecision()) {
                //检查认证识别号
                if (!$_SESSION [C('USER_AUTH_KEY')]) {
                    //跳转到认证网关
                    redirect(PHP_FILE . C('USER_AUTH_GATEWAY'));
                }
                // 没有权限 抛出错误
                if (C('RBAC_ERROR_PAGE')) {
                    // 定义权限错误页面
                    redirect(C('RBAC_ERROR_PAGE'));
                } else {
                    if (C('GUEST_AUTH_ON')) {
                        $this->assign('jumpUrl', PHP_FILE . C('USER_AUTH_GATEWAY'));
                    }
                    // 提示错误信息
                    $this->error(L('_VALID_ACCESS_'));
                }
            }
        }
    }

    function verify(){
        header("Content-Type:text/html; charset=utf-8");
        import("ORG.Util.Image");
        Image::buildImageVerify();
    }


    private function saveSitemap($loc,$model_name,$model_id=null,$priority=0.25,$changefreq="always"){
        $sitemap=M("Sitemap");
        $data["model_name"]=$model_name;
        $data["loc"]=$loc;
        $data["priority"]=$priority;
        $data["lastmod"]=time();
        $data["changefreq"]=$changefreq;
        $data["model_id"]=$model_id;
        $sitemap->add($data);
    }


    function sitemapWriter($m_a,$model_name,$id=null){
        $sitemap=M("Sitemap");
        header("Content-Type:text/html; charset=utf-8");
        $loc=null;
        $sitemap->startTrans();
        if($id==null){
            $loc='http://www.ceefun.com/index.php/'.$m_a;
            $this->saveSitemap($loc,$model_name);
        }else{
            $loc='http://www.ceefun.com/index.php/'.$m_a.'/id/'.$id;
            $this->saveSitemap($loc,$model_name,$id);
        }
        //删除文件
        $sitemaplocation="./sitemap.xml";
        if(file_exists($sitemaplocation)){
            if (!unlink($sitemaplocation)) {
                echo "删除sitemap文件失败!";
                exit;
            }
        }
        //新建基础文件
        $xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
 </urlset>
XML;
        $dom = DOMDocument::loadXML($xmlstr);
        $dom->formatOutput = true;
        $dom->saveXML();
        $dom->save($sitemaplocation);


        //写文件


        if($list=$sitemap->select()){
            $xml_str = file_get_contents($sitemaplocation);
            $dom = DOMDocument::loadXML($xml_str);
            if (!$dom) {
                echo 'Error while parsing the document';
                exit;
            }
            $siteXML=simplexml_import_dom($dom);
            foreach($list as $value){
                $url = $siteXML->addChild('url');
                $url->addChild('loc', $value['loc']);
                $url->addChild('priority', $value['priority']);
                $url->addChild('lastmod', date("Y-m-d H:i:s",time()));
                $url->addChild('changefreq', $value['changefreq']);
            }
            $newxml_str=$siteXML->asXML();
            $dom=DOMDocument::loadXML($newxml_str);
            $dom->formatOutput = true;
            $dom->saveXML();
            $dom->save($sitemaplocation);
        }
        $sitemap->commit();
    }




    function sitemapDeleter($model_name,$id=null){
        header("Content-Type:text/html; charset=utf-8");
        $sitemap=M("Sitemap");
        $data['model_name']=$model_name;
        $data['model_id']=$id;
        $sitemap->startTrans();
        $sitemap->where($data)->delete();
        //删除文件
        $sitemaplocation="./sitemap.xml";
        if(file_exists($sitemaplocation)){
            if (!unlink($sitemaplocation)) {
                echo "删除sitemap文件失败!";
                exit;
            }
        }
        //新建基础文件
        $xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
 </urlset>
XML;
        $dom = DOMDocument::loadXML($xmlstr);
        $dom->formatOutput = true;
        $dom->saveXML();
        $dom->save($sitemaplocation);


        //写文件


        if($list=$sitemap->select()){
            $xml_str = file_get_contents($sitemaplocation);
            $dom = DOMDocument::loadXML($xml_str);
            if (!$dom) {
                echo 'Error while parsing the document';
                exit;
            }
            $siteXML=simplexml_import_dom($dom);
            foreach($list as $value){
                $url = $siteXML->addChild('url');
                $url->addChild('loc', $value['loc']);
                $url->addChild('priority', $value['priority']);
                $url->addChild('lastmod', date("Y-m-d H:i:s",time()));
                $url->addChild('changefreq', $value['changefreq']);
            }
            $newxml_str=$siteXML->asXML();
            $dom=DOMDocument::loadXML($newxml_str);
            $dom->formatOutput = true;
            $dom->saveXML();
            $dom->save($sitemaplocation);
        }
        $sitemap->commit();
    }

}
